package cn.campusshop.article.controller;

import cn.campusshop.article.entity.ArticleDetail;
import cn.campusshop.article.service.ArticleDetailService;
import cn.campusshop.common.entity.Result;
import cn.campusshop.common.entity.StatusCode;
import cn.campusshop.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhulang
 * @Classname ArticleDetailController
 * @Description ArticleDetailController
 */
@RestController
@RequestMapping("article")
public class ArticleDetailController {
    @Autowired
    private ArticleDetailService detailService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("one/{id}")
    public Result findById(@PathVariable int id) {
        ArticleDetail articleDetail = detailService.findById(id);
        if (articleDetail != null) {
            return new Result(true, StatusCode.OK, "查询成功", articleDetail);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("all")
    public Result findAll() {
        List<ArticleDetail> all = detailService.findAll();
        if (all != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", all);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("page/{page}/{size}")
    public Result findAllByPage(@PathVariable(required = false) int page, @PathVariable(required = false) int size) {
        page = page < 1 ? 1 : page;
        size = size < 1 ? 5 : size;
        Page allByPage = detailService.findAllByPage(page, size);
        if (allByPage != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", allByPage);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("page/{page}/{size}/{date}")
    public Result findAllByPageAndRefDate(@PathVariable(required = false) int page, @PathVariable(required = false) int size, @PathVariable(required = false) String date) {
        page = page < 1 ? 1 : page;
        size = size < 1 ? 5 : size;
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        String lastDate = DateUtils.getLastMonth();
        date = date == null || date.trim() == null ? lastDate : date;
        Page byPageAndRefDate = detailService.findAllByPageAndRefDate(page, size, date);
        if (byPageAndRefDate != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", byPageAndRefDate);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("all/{date}")
    public Result findAllByDate(@PathVariable(required = false) String date) {
        String lastDate = DateUtils.getLastMonth();
        date = date == null || date.trim() == null ? lastDate : date;
        List<ArticleDetail> detailList = detailService.findAllByDate(date);
        if (detailList != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", detailList);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("search/{page}/{size}")
    public Result searchByTitle(@PathVariable(required = false) int page, @PathVariable(required = false) int size, @RequestParam(value = "title") String keyword) {
        page = page < 1 ? 1 : page;
        size = size < 1 ? 5 : size;
        Page searchByTitle = detailService.searchByTitle(page, size, keyword);
        if (searchByTitle != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", searchByTitle);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @GetMapping("one")
    public Result findByTitle(@RequestParam("title") String title) {
        ArticleDetail detail = detailService.findByTitle(title);
        if (detail != null) {
            return new Result(true, StatusCode.OK, "查询所有成功", detail);
        } else {
            return new Result(false, StatusCode.ERROR, "查询数据不存在");
        }
    }

    @PutMapping("one")
    public Result update(@RequestBody ArticleDetail articleDetail) {
        Claims claims = (Claims) request.getAttribute("claims_roles");
        int update = detailService.update(articleDetail);
        if (update > 0) {
            return new Result(true, StatusCode.OK, "修改成功");
        } else {
            return new Result(false, StatusCode.ERROR, "修改失败");
        }
    }

    /**
     * 查询指定月份浏览量排名
     *
     * @param num
     * @param date
     * @return
     */
    @GetMapping("top/pv/{num}")
    public Result findTopByPvAndDate(@PathVariable(required = false) int num, @RequestParam(required = false) String date) {
        num = num < 1 ? 3 : num;
        String lastMonth = DateUtils.getLastMonth();
        date = date == null ? lastMonth : date;
        List<ArticleDetail> topByPvAndDate = detailService.findTopByPvAndDate(num, date);
        return new Result(true, StatusCode.OK, "查询成功", topByPvAndDate);
    }

    /**
     * 查询指定月份分享量排名
     *
     * @param num
     * @param date
     * @return
     */
    @GetMapping("top/share/{num}")
    public Result findTopByShareAndDate(@PathVariable(required = false) int num, @RequestParam(required = false) String date) {
        num = num < 1 ? 3 : num;
        String lastMonth = DateUtils.getLastMonth();
        date = date == null ? lastMonth : date;
        List<ArticleDetail> topByShareAndDate = detailService.findTopByShareAndDate(num, date);
        return new Result(true, StatusCode.OK, "查询成功", topByShareAndDate);
    }

    /**
     * 查询指定日期范围的文章数量
     *
     * @param startDate
     * @param endDate
     * @return
     */
    @GetMapping("count")
    public Result findTotalCountByDate(@RequestParam(required = false) String startDate, @RequestParam(required = false) String endDate) {
        String lastMonth = DateUtils.getLastMonth();
        LocalDate now = LocalDate.now();
        int year = now.getYear();
        startDate = startDate == null ? year + "-01" : startDate;
        endDate = endDate == null ? lastMonth : endDate;
        Map<String, Long> totalCountByDate = detailService.findTotalCountByDate(startDate, endDate);
        return new Result(true, StatusCode.OK, "查询成功", totalCountByDate);
    }
}
